package edu.jhu.cs.oose.scheduleit.vo;


import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

/**
 * Entity class of user
 * @author oose_group2
 *
 */
@Entity
public class User implements Comparable<User> {

	/**
	 * The identifier of this class
	 */
	@Id
	@GeneratedValue
	private int id;

	/**
	 * The password of user
	 */
	@Basic
	private String password;

	/**
	 * The detailed information of this user
	 */
	@OneToOne
	private UserInfo userInfo;

	/**
	 * The groups which this user has applied for
	 */
	@SuppressWarnings("unused")
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "User_appliedTeams")
	private List<Group> appliedGroups;

	/**
	 * The groups which this user has joined
	 */
	@SuppressWarnings("unused")
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "User_joinedTeams")
	private List<Group> joinedGroups;


	/**
	 * The getter of id
	 * @return
	 */
	public int getId() {
		return id;
	}

	/**
	 * The getter of password
	 * @return
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * The setter of password
	 * @param password
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * The getter of userInfo
	 * @return
	 */
	public UserInfo getUserInfo() {
		if (this.userInfo == null) {
			this.userInfo = new UserInfo();
		}
		return userInfo;
	}

	/**
	 * The getter of email
	 * @return
	 */
	public String getEmail() {
		return this.userInfo == null ? "" : this.userInfo.getEmail();
	}

	/**
	 * The setter of email
	 * @param email
	 */
	public void setEmail(String email) {
		if (this.userInfo == null) {
			this.userInfo = new UserInfo();
		}
		this.userInfo.setEmail(email);
	}

	/**
	 * The getter of nickname
	 * @return
	 */
	public String getNickName() {
		return this.userInfo == null ? "" : this.userInfo.getNickName();
	}

	/**
	 * The setter of nickname
	 * @param nickName
	 */
	public void setNickName(String nickName) {
		if (this.userInfo == null) {
			this.userInfo = new UserInfo();
		}
		this.userInfo.setNickName(nickName);
	}

	/**
	 * Compare an object with this user
	 * @param obj	The object going to be compared
	 * @return	Whether the input object is equal to this user
	 */
	public boolean equals(Object obj) {
		if (obj instanceof User) {
			return this.getUserInfo().equals(((User) obj).getUserInfo());
		}
		return false;
	}

	@Override
	public int compareTo(User user) {
		if (this.getId() == user.getId()) {
			return 0;
		} else if (this.getId() == 0) {
			return -1;
		} else if (user.getId() == 0) {
			return 1;
		} else {
			return this.getId() > user.getId() ? 1 : -1;
		}
	}

}
